package com.AdvancedDemos.FileAndIO;

import java.io.*;

public class 字节缓冲输入输出流加快文件拷贝 {
    public static void main(String[] args) throws IOException {
        //给字节输入输出流加缓冲buffer,后续IO操作使用缓冲流对象代替输出输出流对象即可
        //内存快,硬盘慢,缓冲区提高了内存和硬盘之间传输的效率
        //字节缓冲输入流 BufferedInputStream
        //BufferedInputStream bis = new BufferedInputStream(new FileInputStream("文件路径"));
        //字节缓冲输出流 BufferedOutputStream
        //BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("文件路径"));

        File file = new File("helloworld\\src\\com\\AdvancedDemos\\FileAndIO\\from\\test.png");
        if (file.length() > Integer.MAX_VALUE) {
            throw new RuntimeException("文件过大");
        }
        FileInputStream from = new FileInputStream(file);
        FileOutputStream to = new FileOutputStream("helloworld\\src\\com\\AdvancedDemos\\FileAndIO\\to\\copy.png");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(from);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(to);
        byte[] bytes = bufferedInputStream.readAllBytes();
        bufferedOutputStream.write(bytes);
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }
}
